package com.chuangshi.core.vote.member.controller.admin;

import com.chuangshi.core.kit.Controller;
import com.chuangshi.core.kit.IdWorker;
import com.chuangshi.core.kit.Result;
import com.chuangshi.core.vote.member.controller.admin.request.*;
import com.chuangshi.core.vote.member.controller.admin.response.MemberAdminV1FindResponse;
import com.chuangshi.core.vote.member.controller.admin.response.MemberAdminV1ListResponse;
import com.chuangshi.core.vote.member.entity.Member;
import com.chuangshi.core.vote.member.service.MemberService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 会员管理端控制器
 *
 * @author marcus
 * @date 2018-11-20
 */
@Api(tags = "会员管理端接口管理")
@RestController
public class MemberAdminController extends Controller {

    @Autowired
    private MemberService memberService;

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "获取会员列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "MemberAdminV1ListRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = MemberAdminV1ListResponse.class)
    })
    @RequestMapping(value = "/vote/member/admin/v1/list", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1List(@ApiIgnore @RequestBody MemberAdminV1ListRequest body) {
        validateRequest(body);

        Result result = memberService.listForAdmin(body);

        validateResponse(MemberAdminV1ListResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "获取单条会员", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "MemberAdminV1FindRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = MemberAdminV1FindResponse.class)
    })
    @RequestMapping(value = "/vote/member/admin/v1/find", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Find(@ApiIgnore @RequestBody MemberAdminV1FindRequest body) {
        validateRequest(body);

        Member result = memberService.find(body.getMemberId());

        validateResponse(MemberAdminV1FindResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "新增会员", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "MemberAdminV1SaveRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功")
    })
    @RequestMapping(value = "/vote/member/admin/v1/save", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Save(@ApiIgnore @RequestBody MemberAdminV1SaveRequest body) {
        validateRequest(body);

        Member member = new Member(body);

        String memberId = IdWorker.getIdStr();
        member.setMemberId(memberId);

        Boolean result = memberService.save(member);

        return renderJson(result, result ? "新增成功" : "新增不成功");
    }

    @ApiOperationSort(value = 40)
    @ApiOperation(value = "修改会员", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "MemberAdminV1UpdateRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功")
    })
    @RequestMapping(value = "/vote/member/admin/v1/update", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Update(@ApiIgnore @RequestBody MemberAdminV1UpdateRequest body) {
        validateRequest(body);

        Member member = new Member(body);

        Boolean result = memberService.update(member, body.getSystemVersion());

        return renderJson(result, result ? "修改成功" : "修改不成功");
    }

    @ApiOperationSort(value = 50)
    @ApiOperation(value = "删除会员", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "MemberAdminV1DeleteRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功")
    })
    @RequestMapping(value = "/vote/member/admin/v1/delete", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Delete(@ApiIgnore @RequestBody MemberAdminV1DeleteRequest body) {
        validateRequest(body);

        Boolean result = memberService.delete(body.getMemberIdList());

        return renderJson(result, result ? "删除成功" : "删除不成功");
    }

}